'use strict';

angular.module('com.dcos.app').controller('DashBoardController', function($window, $scope,$state, $interval,$filter, api, dialog, DashBoardService) {
  var vm = this;
  vm.breadCrumbArr = [api.breadCrumbMap.dashboard.root];

  vm.curProjectId=$window.sessionStorage.getItem("curProjectId");

  vm.userid=$window.sessionStorage.getItem("curUserId");

  // if(!vm.userid)
  //   $state.go('login');
  
  vm.addtime=0;

  vm.pipelineList=[];

  vm.getProjectList = function () {
    
    DashBoardService.getlist(vm.curProjectId).then(function (response) {
      if(vm.pipelineList.length>0) {
        var _pipe=angular.copy(vm.pipelineList);
        vm.pipelineList=response.data.data;
        
        angular.forEach(vm.pipelineList,function(item,index){
          item.success_rate=(item.success_rate*100).toFixed(2).toString();
          item.show=_pipe[index].show;
        });
      }
      else{
        vm.pipelineList=response.data.data;
        
        angular.forEach(vm.pipelineList,function(item,index){
          item.success_rate=(item.success_rate*100).toFixed(2).toString();
          item.show=true;
        });
      }
      vm.addtime=0;
    });
  }
  vm.getProjectList();

  vm.timer = $interval(function () {
    vm.getProjectList();
  }, 1000);


  $scope.$on('$destroy', function () {
    $interval.cancel(vm.timer)
  });

  vm.charts = {
    options: {
      tooltip: {
        formatter: function() {
          return this.points[0].y+"%";
        },
        shared: true
      },
      chart: {
        type: 'column'
      },
      xAxis: {
        categories: []
      },
      yAxis: {
        labels: {
          format: '{value}%'
        },
        title: {
          text: '成功率'
        },
        min: 0,
        max: 100
      },    
      credits: { // 去掉右下角水印
        enabled: false
      },
      exporting: { // 去掉打印图标
        enabled: false
      }
    },
    series: [{
      name: '成功率',
      data: []
    }],
　　title: {
      text: "流水线构建成功率"
    }
  }    
  vm.pipe = {
    options: {
      tooltip: {
        pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
      },
      chart: {
        type: 'pie',
        options3d: {
          enabled: true,
          alpha: 45,
          beta: 0
        }
      },   
      credits: { // 去掉右下角水印
        enabled: false
      },
      exporting: { // 去掉打印图标
        enabled: false
      }
    },
    plotOptions: {
      pie: {
        allowPointSelect: true,
        cursor: 'pointer',
        depth: 35,
        dataLabels: {
          enabled: true,
          format: '{point.name}'
        }
      }
    },
    series: [{
      type: 'pie',
      name: '失败占比',
      data: [
      ]
    }],
　　title: {
      text: "构建失败原因分布"
    }
  }  

  vm.barReport = function () {
    DashBoardService.barReport(vm.curProjectId).then(function (response) {
      vm.appendBar(response.data.data.bar);
    });
  }
  vm.pieReport = function () {
    DashBoardService.pieReport(vm.curProjectId).then(function (response) {
      vm.appendPipe(response.data.data.pie);
    });
  }

  vm.appendBar = function (data) {
    vm.charts.options.xAxis.categories=data.x;
    var yData=[];
    angular.forEach(data.y,function(item,index){
      yData.push(parseFloat(parseFloat(item).toFixed(2)));
    });
    vm.charts.series[0].data=yData;
  }
  vm.appendPipe = function (data) {
    var seriesData=[];
    angular.forEach(data.x,function(item,index){
      var dataArr=[];
      dataArr.push(item);
      dataArr.push(parseFloat(parseFloat(data.y[index]).toFixed(2)));
      seriesData.push(dataArr);
    });
    vm.pipe.series[0].data=seriesData;
  }
  vm.barReport();
  vm.pieReport();
});

